package org.mall.common.util.json;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;

/***
 *@author yinxucun
 *@date 2019-10-18 10:38 
 */
public class JacksonUtils {
    public static Logger logger= LoggerFactory.getLogger(JacksonUtils.class);
    public static String toJSONString(Object o){
        ObjectMapper mapper=new ObjectMapper();
        try {
           return mapper.writeValueAsString(o);
        } catch (JsonProcessingException e) {
           logger.error("解析json失败:\r"+e.getMessage()+"\r\r"+o.toString());
        }
        return "";
    }

    public static Object parseObject(String text, Class mclass){
        ObjectMapper mapper=new ObjectMapper();
        try {
            return mapper.readValue(text,mclass) ;
        } catch (IOException e) {
            logger.error("反序列化失败:\r"+e.getMessage()+"\r\r 待解析文本"+text);
            e.printStackTrace();
        }
        return null;
    }


}
